package top.xia17.plugins.fast.common.model;

import lombok.Data;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.MultiValueMap;

import jakarta.validation.constraints.NotNull;

/**
 * 分页参数
 * @author xia17
 * @date 2020/7/27 14:29
 */
@Data
public class PageParam {

    /**
     * 页数
     */
    @NotNull(message = "页数不能为空")
    private Integer page;

    /**
     * 数量
     */
    @NotNull(message = "数量不能为空")
    private Integer size;

    /**
     * 获取分页
     */
    public Pageable toPageable(){
        int page , size ;
        page = this.page == null ? 0 : this.page - 1;
        size = this.size == null ? 8 : this.size;
        return PageRequest.of(page,size);
    }

    /**
     * 获取分页
     * @param sort 排序
     */
    public Pageable toPageable(Sort sort){
        int page , size ;
        page = this.page == null ? 0 : this.page - 1;
        size = this.size == null ? 8 : this.size;
        return PageRequest.of(page,size,sort);
    }

    public void pushToParam(MultiValueMap<String, String> params){
        params.add("page",this.page.toString());
        params.add("size",this.size.toString());
    }


}
